/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.mnt.util;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import com.google.common.collect.Maps;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 远程执行linux命令
 *
 * @author: ZhangHouYing
 * @date: 2019-08-10 10:06
 */
public class ScpClientUtil {

    static private final Map<String, ScpClientUtil> CLIENT_UTIL_HASH_MAP = Maps.newHashMap();
    private static volatile ScpClientUtil instance;
    /**
     * 单例模式
     * 懒汉式
     * 线程安全
     * @return
     */
    public static ScpClientUtil getInstance(){
        if(null == instance){
            synchronized (ScpClientUtil.class){
                if(null == instance){
                    instance = new ScpClientUtil();
                }
            }
        }
        return instance;
    }

    public static ScpClientUtil getInstance2(String ip,int port,String name,String password){
        if(null == instance){
            synchronized (ScpClientUtil.class){
                if(null == instance){
                    instance = new ScpClientUtil(ip,port,name,password);
                }
            }
        }
        return instance;
    }

    static synchronized public ScpClientUtil getInstance(String ip, int port, String username, String password) {
        if (CLIENT_UTIL_HASH_MAP.get(ip) == null) {
            CLIENT_UTIL_HASH_MAP.put(ip, new ScpClientUtil(ip, port, username, password));
        }
        return CLIENT_UTIL_HASH_MAP.get(ip);
    }
    /**
     * 私有化默认构造函数
     * 实例化对象只能通过getInstance
     */
    private ScpClientUtil(){

    }
    public ScpClientUtil(String ip, int port, String username, String password) {
        this.ip = ip;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public void getFile(String remoteFile, String localTargetDirectory) {
        Connection conn = new Connection(ip, port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);
            if (!isAuthenticated) {
                System.err.println("authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            client.get(localTargetDirectory + "/" + remoteFile);
        } catch (IOException ex) {
            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            conn.close();
        }
    }

    public void putFile(String localFile, String remoteTargetDirectory) {
        putFile(localFile, null, remoteTargetDirectory);
    }

    public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory) {
        putFile(localFile, remoteFileName, remoteTargetDirectory, null);
    }

    public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) {
        Connection conn = new Connection(ip, port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);
            if (!isAuthenticated) {
                System.err.println("authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            if ((mode == null) || (mode.length() == 0)) {
                mode = "0600";
            }
            File file = new File(localFile);
            if (remoteFileName == null) {
                client.put(file.getName(), file.length(), remoteTargetDirectory, mode);
            } else {
                client.put(remoteFileName, file.length(), remoteTargetDirectory, mode);
            }
        } catch (IOException ex) {
            Logger.getLogger(ScpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            conn.close();
        }
    }

    private String ip;
    private int port;
    private String username;
    private String password;


}
